/**********************************************************************/
/*
   Author: Karan Makkar
   Created: August 2023
   Description: Program to load in and save .dbf files
*/
/**********************************************************************/

* include filepaths 
  if "$master_run" !="1" include "./Do/SET_FILEPATHS.do"
  cap log close
	global prefix: display %tdCYND td(`c(current_date)')
	log using "$KP_logs/${prefix}_load_dbfs.txt", text replace

  clear all
  set more off

*Define program
program define load_dbfs

    args input output
    import dbase "`input'", clear

    sa "`output'", replace

end 

/**************************
*March 2022 Susenas
**************************/
local files : dir "$KP_deid_susenas/Raw/Household/dbf/mar22" files "*.dbf"
foreach file in `files'{
    local filename = subinstr("`file'", ".dbf", "", .)
    load_dbfs "$KP_deid_susenas/Raw/Household/dbf/mar22/`file'" "$KP_deid_susenas/Raw/Household/mar22_`filename'"
}

forvalues i = 41/43{
  local files : dir "$KP_deid_susenas/Raw/Household/" files "mar22_blok`i'*.dta"
  local j = 1
  foreach file in `files'{
    local file_path = "${KP_deid_susenas}/Raw/Household/`file'"
    if  `j' ==1 use "`file_path'", clear
    if `j' !=1 append using "`file_path'"
    erase "`file_path'"
    local j = `j'+1
  }

  sa "$KP_deid_susenas/Raw/Household/Block`i'-mar2022.dta", replace
}

